#include <linux/printk.h>
#include <linux/platform_device.h>
#include <linux/module.h>

#include "chip_demo_gpio_led.h"
#include "board_led.h"
#include "led_drv.h"


//struct led_resource *led_board_A_res;

int led_gpio_pins[10];  //保存引脚
int led_cnts;    		//记录引脚数量

/*
	接收 led_drv.c 传递的初始化引脚参数（接收led_drv.c传递的次设备号）
	负责初始化GPIO
*/
static int led_chip_init(int which)
{
	printk("%s: control led of %d\n", __FUNCTION__, which);

	printk("init group %d, pin %d\n", GROUP(led_gpio_pins[which]), pin(led_gpio_pins[which]));
	
	return 0;
}


/*
	接收 led_drv.c 传递的控制引脚参数
	负责控制GPIO输出高低电平
*/
static int led_chip_ctl(int which, char status)
{
	printk("%s: control led of %d, status = %d\n", __FUNCTION__, which, status);

	printk("init group %d, pin %d\n", GROUP(led_gpio_pins[which]), pin(led_gpio_pins[which]));
	
	return 0;
}

	
/*
	led_chip_operations 结构体变量
	定义了GPIO个数
		  GPIO的初始化和控制函数指针
*/
static struct led_chip_operations led_chip_ops = {
	.led_init = led_chip_init,
	.led_ctl  = led_chip_ctl,
};

static int chip_led_probe(struct platform_device *dev)
{
	/* 记录引脚 */
	struct resource *res;
	printk("This is chip_led_probe function\n");
	while(1)
	{
		res = platform_get_resource(dev, IORESOURCE_IRQ, led_cnts);
		if(!res)
			break;
		led_gpio_pins[led_cnts] = res->start;
		/* 创建设备节点 */
		led_device_create(led_cnts);
		led_cnts++;
	}
	
	return 0;
}

static int chip_led_remove(struct platform_device *dev)
{
	int i;
	printk("This is chip_led_remove function\n");
	
	/* 卸载设备节点 */
	for(i=0; i<led_cnts; i++)
	{
		led_device_destroy(i);
	}
	led_cnts = 0;
	
	return 0;
}

static struct platform_driver chip_led_platform_drv = {
	.probe		= chip_led_probe,
	.remove		= chip_led_remove,
	.driver		= {
		.name	= "imx6ull_leds",
	},
};

static int chip_led_platform_init(void)
{
	int err;
	err = platform_driver_register(&chip_led_platform_drv);
	get_led_resources(&led_chip_ops);

	return 0;
}

static void chip_led_platform_exit(void)
{
	platform_driver_unregister(&chip_led_platform_drv);
}

module_init(chip_led_platform_init);
module_exit(chip_led_platform_exit);
MODULE_LICENSE("GPL");



